package com.mall.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.hadoop.classification.InterfaceAudience.Public;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.bean.Goods;
import com.mall.bean.Leave_msg;
import com.mall.bean.User;
import com.mall.service.IDetailService;

@Controller
public class DetailController {

	@Resource
	private IDetailService iDetailService;


	//展示modal
	@RequestMapping("showModal")
	@ResponseBody
	public Map<String, Object> showModal(@Param("goods_id") String goods_id, Goods goods, HttpSession session) {


		Map<String, Object> resMap = new HashMap<String, Object>();
		Goods modal = iDetailService.showModal(goods);
		resMap.put("modal", modal);

		return resMap;

	}

	//展示详情
	@RequestMapping("showDetail")
	public String showDetail(@Param("goods") String goods, @Param("goods_type") String goods_type, @Param("goods_id") Integer goods_id, Model model, Leave_msg leave_msg, HttpSession session) {

		User user = (User) session.getAttribute("user");
		System.out.println(goods);
		Goods good = new Goods();
		good.setGoods_id(goods_id);
		Leave_msg parentLast = iDetailService.findParentLast(leave_msg);
		List<Leave_msg> comment = iDetailService.showParent2(leave_msg);
		model.addAttribute("parentLast", parentLast);
		model.addAttribute("comment", comment);
		model.addAttribute("user", user);
		if ("服装".equals(goods)) {
			good.setGoods_type(goods_type);
			Goods detail = iDetailService.goodsDetail(good);
			model.addAttribute("detail", detail);
			return "product-detail-clothing";
		} else if ("其它".equals(goods)) {
			good.setGoods(goods);

			Goods detail = iDetailService.goodsDetail(good);
			model.addAttribute("detail", detail);
			return "product-detail-other";
		} else if ("手机".equals(goods)) {
			good.setGoods(goods);
			Goods detail = iDetailService.goodsDetail(good);
			model.addAttribute("detail", detail);
			return "product-detail-phone";
		} else {

			Goods detail = iDetailService.goodsDetail(good);
			model.addAttribute("detail", detail);

			return "product-detail-1";
		}
	}

	//留言
	@RequestMapping("leave_msg")
	@ResponseBody
	public Map<String, Object> leave_msg(@Param("parent_code") String parent_code, @Param("parent_id") Integer parent_id, @Param("message") String message, @Param("to_user_id") Integer to_user_id, @Param("goods_id") Integer goods_id, Leave_msg leave_msg, Model model, HttpSession session) {

		Map<String, Object> resMap = new HashMap<String, Object>();

		User user = (User) session.getAttribute("user");
		System.out.println("1:" + parent_code);
		Date time = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowdata = ft.format(time);
		//随机生成不重复的字母
		String word = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String code = "";
		for (int i = 0; i < 10; i++) {
			int rand = (int) (Math.random() * word.length());
			char c = word.charAt(rand);
			if (!code.contains(c + "")) {
				code += c;
			} else {
				i--;
			}
		}
		leave_msg.setDate(nowdata);
		leave_msg.setContent(message);
		leave_msg.setTo_user_id(to_user_id);
		leave_msg.setGoods_id(goods_id);
		leave_msg.setFrom_user_id(22);
		leave_msg.setCode(code);
		//提交留言
		if (parent_id == null) {

			iDetailService.addParent(leave_msg);
			Leave_msg leaveMsg = iDetailService.findParent_id(leave_msg);
			iDetailService.addChildren(leaveMsg);
			resMap.put("leaveMsg", leaveMsg);
			return resMap;

		}
		//回复
		else {
			leave_msg.setCode(parent_code);
			Leave_msg parent = iDetailService.findParent_id(leave_msg);
			//查找父评论中是否有子评论
			List<Leave_msg> isMsg = iDetailService.showChild(leave_msg);
			System.out.println("size:" + isMsg.size());
			//随机生成不重复的字母
			String word2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			String code2 = "";
			for (int i = 0; i < 10; i++) {
				int rand = (int) (Math.random() * word2.length());
				char c = word.charAt(rand);
				if (!code2.contains(c + "")) {
					code2 += c;
				} else {
					i--;
				}
			}
			leave_msg.setCode(code2);
			System.out.println("parent:" + parent);
			//判断回复的当前行是不是父评论，以及父评论下有没有子评论
			if (parent == null || isMsg.size() > 1) {
				//append
				leave_msg.setParent_id(parent_id);
				iDetailService.addChildren(leave_msg);

				Leave_msg leaveMsg = iDetailService.findChild_id(leave_msg);
				Leave_msg childLast = iDetailService.findChildLast(leave_msg);
				System.out.println(leave_msg);
				resMap.put("childLast", childLast);
				resMap.put("leaveMsg", leaveMsg);

			} else {
				//更新
				leave_msg.setCode(code2);
				iDetailService.addChildren(leave_msg);
				List<Leave_msg> parentMsg = iDetailService.showParent2(leave_msg);
				Leave_msg childLast = iDetailService.findChildLast(leave_msg);
				resMap.put("parentMsg", parentMsg);
				resMap.put("childLast", childLast);
				return resMap;
			}

		}
		return resMap;
	}

	//展示子评论
	@RequestMapping("showchildren")
	@ResponseBody
	public Map<String, Object> showchildren(@Param("parent_id") Integer parent_id, @Param("message") String message, @Param("to_user_id") Integer to_user_id, @Param("goods_id") Integer goods_id, Leave_msg leave_msg, Model model, HttpSession session) {

		leave_msg.setParent_id(parent_id);
		Map<String, Object> res = new HashMap<String, Object>();
		List<Leave_msg> childMsg = iDetailService.showChild(leave_msg);
		Leave_msg childLast = iDetailService.findChildLast(leave_msg);
		res.put("childMsg", childMsg);
		res.put("childLast", childLast);
		System.out.println(childMsg);
		return res;
	}

	//删除留言
	@RequestMapping("delete_Msg")
	@ResponseBody
	public Map<String, Object> delete_Msg(@Param("goods_id") Integer goods_id, @Param("parent_id") Integer parent_id, @Param("from_user_id") Integer from_user_id, @Param("code") String code, Leave_msg leave_msg, HttpSession session) {
		User user = (User) session.getAttribute("user");
		Map<String, Object> res = new HashMap<String, Object>();
		leave_msg.setParent_id(parent_id);
		leave_msg.setCode(code);
		leave_msg.setGoods_id(goods_id);
		if (from_user_id == user.getUser_id()) {
			//删除父留言
			iDetailService.delete_PMsg(leave_msg);
			iDetailService.delete_CMsg(leave_msg);
			List<Leave_msg> parentMsg = iDetailService.showParent2(leave_msg);
			Leave_msg childLast = iDetailService.findChildLast(leave_msg);
			System.out.println(parentMsg);
			res.put("parentMsg", parentMsg);
			res.put("childLast", childLast);
			return res;
		} else {
			res.put("error", "该留言不是您的不能删除");
			return res;
		}
	}
}
